<?php
error_reporting(E_ALL ^ E_NOTICE);
ini_set('memory_limit', '1000M');
set_time_limit(0);
CONST TYPE_NORMAL = 1;
CONST TYPE_BAD = 2;
include_once('function.php');
include_once('sell.php');
include_once('rent.php');
$borough_file_name = 'borough';
$borough_file=file($borough_file_name);
$subwaytoborough_filename = 'subwaytoborough';
$subwaytoborough_file = file($subwaytoborough_filename);
$borough_price_file_name = 'borough_price';
foreach($subwaytoborough_file as &$line_json){
    $line = json_decode($line_json, True);
    $subwaytoborough[$line['bid']] = $line['id'];
}

function outputBadHouse($house_info, $bad_type){
    $house_info['bad_type'] = $bad_type;
    $out = json_encode($house_info);
    echo ' '."\t".$out."#B\n";
}
$borough = array();
foreach($borough_file as &$line_json){
    $line = json_decode($line_json, True);
    $borough[$line['borough_name']] = 
	array('id'=>$line['id'], 'cityarea_id'=>$line['cityarea_id'], 'cityarea2_id'=>$line['cityarea2_id']);
}
$borough_price_file = file($borough_price_file_name);
foreach ($borough_price_file as &$line_json){
    $line = json_decode($line_json, True);
    $borough_price[$line['borough_id']] = $line['price'];
}
$stat_array=array();
$borough_array=array();
$source_array=array();
while (($line_json = fgets(STDIN)) !== false) {
    $line_json = trim($line_json);
    if(empty($line_json))
        continue;
    $house_info = json_decode($line_json, true);
    $house_info['tag'] = str_replace("12#", "", $house_info['tag']);
    
    //处理小区
    $house_info['borough_name'] = trim($house_info['borough_name']);
    if ($borough[$house_info['borough_name']]){
	    $house_info['borough_id'] = $borough[$house_info['borough_name']]['id'];
        $house_info['cityarea_id'] = $borough[$house_info['borough_name']]['cityarea_id'];
        $house_info['cityarea2_id'] = $borough[$house_info['borough_name']]['cityarea2_id'];
        $is_subway = $subwaytoborough[$house_info['borough_id']];
        if(!empty($is_subway)){
            $house_info['tag'] = $house_info['tag'].'12#';
        }
        $house_info['tag'] = str_replace("1001#", "", $house_info['tag']);
        if ($house_info['house_totalarea'] > 0){
        $hprice=$house_info['house_price']/$house_info['house_totalarea'];
        if ($borough_price[$house_info['borough_id']]){
            $bprice=$borough_price[$house_info['borough_id']];
            $price_ratio =$house_info['house_price']/$house_info['house_totalarea'] / $bprice;
            if ($hprice > 1000 || $hprice < 10 || $price_ratio > 2 || $price_ratio < 0.5){
                $house_info['tag'] = "1001#".$house_info['tag'];
            }
        }
        else if ($hprice > 1000 || $hprice < 10){
            $house_info['tag'] = "1001#".$house_info['tag'];
        }
        $house_info['tag'] = str_replace("1003#", "", $house_info['tag']);
        if ($house_info['house_totalarea'] < 10 || $house_info['house_totalarea'] > 1000){
            $house_info['tag'] = "1003#".$house_info['tag'];
        }

    }

    }
    else{
        #if ($house_info['source'] != 10){
            #echo $house_info['source']."\n";
            #echo $house_info['borough_name']."\n";
        #}
	    outputBadHouse($house_info, 19);
	    continue;
    }
    $out = json_encode($house_info);
    echo "haha\t".$out."#N\n";
}
    

    
